package com.handinfo.uimenu;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.util.Tools;
import com.test.touch.GameTouchManager;

public class UIDialogMenu extends UIAbstractContext
{
	private final int IW = 2;
	//Data
	private String m_title;
	private byte m_type_describe;
	private Object m_data_describe;
	private byte[] m_types_items;
	private Object[] m_datas_items;
	//Dis
	private final int H_TITLE = UITools.H_PRE;
//	private final int H_PAGE = UITools.H_PRE;
	private int m_h_describe;
	private int m_w_rect;
	//Logic
	private int m_y_top;
	private int m_index_top;
	private int m_index;//当前选中的Item
	private boolean m_isSupportDrag;// 是否支持拖拉
//	private boolean m_isSupprotTurnUD = true;// 是否支持上下来回翻页，如为true 则0->N;N->0
//	private boolean m_isSupprotTurnLR = true;// 是否支持左右来回翻页，如为true 则0->N;N->0

	protected UIDialogMenu(int id)
	{
		super(id);
	}

	/**
	 * 设置数据
	 * 
	 * @param title
	 * @param describe
	 * @param menus
	 */
	public void setContents(String title, String describe, String[] menus)
	{
		//clean vars
		m_index_top = 0 ;
		m_index = 0 ;
		//
		m_w_content = W_MAX_CONTENT;
		m_w_rect = m_w_content + (IW << 1);
		m_w = m_w_rect + (INITX_CONTENT << 1);
		m_title = title;
		//计算描述层的高度，及设置描述信息
		int sumH_describe = 0;
		if (describe == null) {
			m_data_describe = null;//修改时注销
		}
		else {
			if (AbsHyperLinker.isHyperLink(describe)) {
				AbsHyperLinker hyperLinker = AbsHyperLinker.createHyperLinker(describe, m_w_content, 200);
				int h = hyperLinker.getRealHeight();
				hyperLinker.setH(h);
				m_type_describe = TYPE_STR_HYPER;
				m_data_describe = hyperLinker;
				sumH_describe += h;
			}
			else {
				if (AbstractGameScreen.s_font.stringWidth(describe) > m_w_content) {//多行文字
					String[] strs = Tools.getStrArray(describe, AbstractGameScreen.s_font, m_w_content);
					m_type_describe = TYPE_STR_NORS;
					m_data_describe = strs;
					sumH_describe += strs.length * UITools.H_PRE;
				}
				else {//单行文字
					m_type_describe = TYPE_STR_NOR;
					m_data_describe = describe;
					sumH_describe += UITools.H_PRE;
				}
			}
		}
		m_h_describe = sumH_describe;
		//计算内容层的高度，及设置内容
		int sumH_items = 0;
		if (menus == null) {
			m_datas_items = null;
		}
		else {
			m_types_items = new byte[menus.length];
			m_datas_items = new Object[menus.length];
			for (int i = 0; i < menus.length; i++) {
				if (menus[i] == null) {
					throw new IllegalArgumentException("未传入参数 index = " + i);
				}
				if (AbsHyperLinker.isHyperLink(menus[i])) {
					AbsHyperLinker hyperLinker = AbsHyperLinker.createHyperLinker(menus[i], m_w_content, 200);
					int h = hyperLinker.getRealHeight();
					hyperLinker.setH(h);
					m_types_items[i] = TYPE_STR_HYPER;
					m_datas_items[i] = hyperLinker;
					sumH_items += h;
				}
				else {
					if (AbstractGameScreen.s_font.stringWidth(menus[i]) > m_w_content) {//多行文字
						String[] strs = Tools.getStrArray(menus[i], AbstractGameScreen.s_font, m_w_content);
						m_types_items[i] = TYPE_STR_NORS;
						m_datas_items[i] = strs;
						sumH_items += strs.length * UITools.H_PRE;
					}
					else {//单行文字
						m_types_items[i] = TYPE_STR_NOR;
						m_datas_items[i] = menus[i];
						sumH_items += UITools.H_PRE;
					}
				}
			}
			int maxItemHeight = getMaxHeightOfItems();//当前可获得的最大Item高度
//			sumH_items = sumH_items >= maxItemHeight ? maxItemHeight : sumH_items;
			boolean tempIsSupportDrag;
			if (sumH_items >= maxItemHeight) {
				sumH_items = maxItemHeight;
				tempIsSupportDrag = true;
			}
			else {
				tempIsSupportDrag = false;
			}
			m_isSupportDrag = tempIsSupportDrag;
			if (m_isSupportDrag) {
				m_w += UIManager.IMAGE_SCROLL_ARR.getWidth();
			}
		}
		m_h_content = sumH_items;
		m_h = H_TITLE + m_h_describe + m_h_content + (INITY_CONTENT << 1) + INITY_CONTENT;//压边，多加一个单位的initY
		setCentre();
		
		registerTouch();
	}

	/**
	 * 设置是否支持翻页
	 * @param isSupportPage
	 */
//	public void setSupprotPage(boolean isSupportPage)
//	{
//		if (m_isSupportPage == isSupportPage) {
//			return;
//		}
//		m_isSupportPage = isSupportPage;
//	}

	/**
	 * 获得最大的Items高度，
	 * 此高度由剩余宽度决定
	 * @return
	 */
	private int getMaxHeightOfItems()
	{
		int MAXH = (AbstractGameScreen.SCREEN_HEIGHT << 1) / 3;//显示最大为屏幕的2/3
		int tempH = MAXH;
		tempH -= H_TITLE;//减去标题的高度
		if (m_data_describe != null) {
			tempH -= m_h_describe;
		}
		if (tempH < UITools.H_PRE) {//若描述占据的空间过大
			throw new IllegalArgumentException("文字占据了太多的控件，不符合系统要求");
		}
		return (tempH / UITools.H_PRE) * UITools.H_PRE;//获得整数条item
	}

	protected void keyLogic()
	{
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)
				||AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
			close();
			return;
		}
		if (m_datas_items == null) {
			return;
		}
		if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)) {
			if (m_index == 0) {
				return;
			}
			if (m_index == m_index_top) {//显示最顶
				if (m_index_top != 0) {
					m_index--;
					m_index_top = m_index;
					m_y_top = 0;
				}
			}
			else {
				m_index--;
				if (m_index == m_index_top) {
					m_y_top = 0;
				}
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)) {
			if (m_index < m_datas_items.length - 1) {
				m_index++;
				if (!isItemDisAll(m_index)) {//如果下一位置不在屏幕范围内
					int sumH = 0;
					int i = m_index;
					for (; i >= 0; i--) {
						sumH += getDataHeight(m_types_items[i], m_datas_items[i]);
						if (sumH >= m_h_content) {
							break;
						}
					}
					if (sumH < m_h_content) {
						return;
					}
					else {
						m_y_top = m_h_content - sumH;
						m_index_top = i;
					}
				}
			}
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT)) {
		}
		else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT)) {
		}
	}

	/**
	 * 判断当前项是否显示完全
	 * @param index
	 * @return
	 */
	private boolean isItemDisAll(int index)
	{
		int sumH = m_y_top;
		for (int i = m_index_top; i <= index; i++) {
			sumH += getDataHeight(m_types_items[i], m_datas_items[i]);
		}
		return sumH <= m_h_content;
	}

	protected void paintContent(Graphics g)
	{
		paintBackground(g);
		paintDescribe(g);
		paintItems(g);
		paintDrag(g);
		paintPage(g);
	}

	private void paintBackground(Graphics g)
	{
		int h = UIManager.IMAGE_TITlE_MIDDLE.getHeight();
		UITools.drawWindowBackgroud(g, m_x, m_y + h - 5, m_w, m_h - h);
		//标题压住底边框
		if (m_title == null) {
			UITools.paintBackgroundTitleOn240320(g, m_x, m_y, m_w, m_h,"");
		}
		else {
			UITools.paintBackgroundTitleOn240320(g, m_x, m_y, m_w, m_h, m_title);
		}
	}

	private void paintDescribe(Graphics g)
	{
		if (m_data_describe == null) {
			return;
		}
		int tempX = m_x + INITX_CONTENT;
		int tempY = m_y + INITY_CONTENT + H_TITLE;
		paintContentLayer(g, tempX, tempY,UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS), m_type_describe, m_data_describe);
	}

	private void paintItems(Graphics g)
	{
		if (m_datas_items == null || m_datas_items.length == 0) {
			return;
		}
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		//
		int tempX = m_x + INITX_CONTENT;
		int tempY = getItemsInitY();
		g.setClip(tempX, tempY, m_w_rect, m_h_content);
		paintItems(g, tempX, tempY);
		//
		g.setClip(clipX, clipY, clipW, clipH);
	}

	private void paintItems(Graphics g, int x, int y)
	{
		int tempX = x;
		int tempY = y + m_y_top;
		int currentIndex = getCurrentIndex();
		for (int i = m_index_top; i < m_datas_items.length; i++) {
			if (m_datas_items[i] == null) {
				continue;
			}
			int color = 0;
			int dataH = getDataHeight(m_types_items[i], m_datas_items[i]);
			if (i == currentIndex) {
				paintSelItem(g, tempX, tempY, m_w_rect, dataH);//赵铮
				color = UIManager.getPaletteColor(UIManager.COLOR_WORD_YESS);
			}
			else {
				color =UIManager.getPaletteColor(UIManager.COLOR_WORD_NOS);
			}
			int contentX = tempX + IW;
			paintContentLayer(g, contentX, tempY + 2, color, m_types_items[i], m_datas_items[i]);
			tempY += dataH;
		}
		g.setClip(0, 0, AbstractGameScreen.SCREEN_WIDTH,AbstractGameScreen.SCREEN_HEIGHT);
	}

	private void paintDrag(Graphics g)
	{
		if (!m_isSupportDrag) {
			return;
		}
		if (m_datas_items == null || m_datas_items.length == 0) {
			return;
		}
		int len = 0;
		if (m_index == 0) {
			len = 0;
		}
		else if (m_index == m_datas_items.length - 1) {
			len = 100;
		}
		else {
			len = (100 * (m_index + 1)) / m_datas_items.length;
		}
	
//		UITools.paintScrollBar(g, m_x + m_w - UIManager.IMAGE_SCROLL_RULING.getWidth() - UITools.INITX_CONTENT, getItemsInitY(),
//				m_h_content, len);
		UITools.paintScrollBar(g, UITools.getDragX(m_x , m_w), getItemsInitY(),m_h_content, len) ;
	}
	
	private int getItemsInitY(){
		int tempY = m_y + INITY_CONTENT;
		tempY += H_TITLE;
		if (m_data_describe != null) {
			tempY += m_h_describe;
		}
		return tempY ;
	}

	private void paintPage(Graphics g)
	{

	}

	public int getCurrentIndex()
	{
		return m_index;
	}

	protected void registerTouch() {
		clearAllRects();
		if (m_datas_items == null || m_datas_items.length == 0) {
			int[][] rect = new int[][]{{m_x, m_y, m_w, m_h, -1}};
			initTouchRect(rect);
		} else {
			int[][] rect = new int[m_datas_items.length - m_index_top +1][];
			int tempX = m_x + INITX_CONTENT;
			int tempY = getItemsInitY();
	
			tempY += m_y_top;
			for (int i = m_index_top; i < m_datas_items.length; i++) {
				int dataH = getDataHeight(m_types_items[i], m_datas_items[i]);
				rect[i-m_index_top] = new int[]{tempX, tempY, m_w_rect, dataH};
				if (m_datas_items[i] == null) {
					continue;
				}
				tempY += dataH;
			}
			rect[rect.length-1] = new int[]{m_x, m_y, m_w, m_h, -1};
			initTouchRect(rect);
		}
	}

	protected void onTouchRect(int[][] rect, int touchid) {
		if (touchid == rect.length-1) {
			GameTouchManager.keyPressedRSoft();
		} else {
			if (m_index == touchid) {
				GameTouchManager.keyPressedFire();
			} else {
				m_index = touchid;
			}
		}
	}

	
	
}
